package randomnumber;

import java.lang.annotation.Target;

public class RandomNumber {
	
	
	public static void main(String[] args) throws Exception {
		
		int start=2;
		int end=11;
		int special=7;
		double prob=0.4;
		int[] count = new int[end+1];
		int rn;
		for (int i = 0; i < 100000; i++) {
			rn=getRandomNumber(start, end, special, prob);
			count[rn]++;
		}
		
		for (int j = start;j <= end; j++){
			System.out.println(j+" : "+count[j]);
		}
		
		
	}
	
	public static int getRandomNumber(int start,int end,int special,double prob) throws Exception{
		if(start>end)throw new Exception("start can't be greater than end!");
		if(special<start||special>end)throw new Exception("special should be between start and end!");
		double range=end-start+1.0;
		double x=(prob-(1.0/range))/((range-1.0)/range);
		if(x>1||x<0)throw new Exception("impossible!");
		int rand;
		rand=(int)(Math.random()*(end-start+1))+start;
		if(Math.random()<x)rand=special;
		return rand;
	}
	
}
